package com.ufcg.lvc.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.ufcg.lvc.client.prototipacao.Cadastro;
import com.ufcg.lvc.client.prototipacao.Inicio;

/**
 * 
 * Entry point classes define <code>onModuleLoad()</code>.
 * 
 * Esta é a classe inicial do programa. É aqui onde o usuário vai 
 * pedir para logar ou cadastra-se no sistema.
 * 
 * @author Larissa Costa
 * 
 */
public class Teste__ implements EntryPoint {

	public static GreetingServiceAsync getServico() {
		return GWT.create(GreetingService.class);
	}
	private UserData UD;
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {

		final RootPanel rootPanel = RootPanel.get();
		rootPanel.setSize("1235", "719");

		final AbsolutePanel panelPrincipal = new AbsolutePanel();
		rootPanel.add(panelPrincipal, 304, 146);
		panelPrincipal.setSize("416px", "233px");

		Label lblCorrigeFcil = new Label("Corrige Fácil");
		lblCorrigeFcil.setStyleName("gwt-TituloGrande");
		panelPrincipal.add(lblCorrigeFcil, 55, 10);

		Label lblUsurio = new Label("Login:");
		panelPrincipal.add(lblUsurio, 10, 118);

		Label lblSenha = new Label("Senha:");
		panelPrincipal.add(lblSenha, 10, 151);

		final TextBox tbUsuario = new TextBox();
		panelPrincipal.add(tbUsuario, 90, 110);
		tbUsuario.setSize("302px", "17px");

		final PasswordTextBox tbSenha = new PasswordTextBox();
		panelPrincipal.add(tbSenha, 90, 145);
		tbSenha.setSize("302px", "17px");

		Button btEntrar = new Button("Entrar");
		btEntrar.setText("Entrar");
		panelPrincipal.add(btEntrar, 159, 194);
		btEntrar.setSize("91px", "29px");

		Button btRegistreseAqui = new Button("Registre-se aqui");
		btRegistreseAqui.setStyleName("gwt-Button_invisivel");
		panelPrincipal.add(btRegistreseAqui, 284, 175);

		btEntrar.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				getServico().validaUsuarioSenha(tbUsuario.getText(),
						tbSenha.getText(), new AsyncCallback<String[]>() {
							
							@Override
							public void onSuccess(String[] result) {
								if (result== null) {
									Window.alert("Erro: Usuário e senha inválidos.");
								} else {
									UD = UserData.getInstance();
									
									UD.setId_usuario(result[0]);
									UD.setNome_usuario(result[1]);
									UD.setLogin_usuario(tbUsuario.getText());
									
									System.out.println("setId_usuario"    + (result[0]));
									System.out.println("setNome_usuario"  + (result[1]));
									System.out.println("setLogin_usuario" + (tbUsuario.getText()));
									
									rootPanel.clear();
									final Inicio i = new Inicio(UD);
									rootPanel.add(i.getPanel(), 0, 0);
								}
							}
							
							@Override
							public void onFailure(Throwable caught) {
								Window.alert("Erro na comunicação com o servidor");						
							}
						});
			}
		});

		btRegistreseAqui.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				rootPanel.clear();
				Cadastro i = new Cadastro();
				rootPanel.add(i.getPanel(), 0, 0);
			}
		});
	}

}
